<template>
  <div>
    <table>
      <tr>
        <td>不良品项编号</td>
        <td>
          <el-input
            v-model="form.Defective_BianHao"
            placeholder="请输入内容"
          ></el-input>
        </td>
        <td>不良品项名称</td>
        <td>
          <el-input
            v-model="form.Defective_Name"
            placeholder="请输入内容"
          ></el-input>
        </td>
      </tr>

      <tr>
        <td>附件</td>
        <td>
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList"
            v-model="form.Defective_attachments"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </td>
        <td>照片</td>
        <td>
          <el-upload
            class="avatar-uploader"
            action="http://localhost:5255/api/IDefective/UploadFile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="form.Defective_Img"
              :src="'http://localhost:5255/PIc/' + form.Defective_Img"
              class="avatar"
              width="100px"
              height="100px"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </td>
      </tr>
      <tr>
        <td><el-button type="success" @click="Add()">添加</el-button></td>
      </tr>
    </table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        Defective_BianHao: "",
        Defective_Name: "",
        Defective_attachments: "jpg,png",
        Defective_Img: "",
      },

      fileList: [
        {
          name: "food.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
        {
          name: "food2.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
      ],
    };
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },

    handleAvatarSuccess(res) {
      this.form.Defective_Img = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    //添加
    Add() {
      this.axios
        .post("http://localhost:5255/api/IDefective/AddDefective", this.form)
        .then((cxx) => {
          if (cxx.data > 0) {
            this.$message.success("添加成功");
            //关闭弹窗
            this.$emit("clickDefAdd");
          } else {
            this.$message.error("添加失败");
          }
        });
    },
  },

  created() {},
};
</script>
<style>
</style>